﻿
namespace Aeon.Emulator.Instructions.BitwiseLogic
{
    internal static class Bsr
    {
        [Opcode("0FBD/r rw,rmw", OperandSize = 16, AddressSize = 16 | 32)]
        public static void BitScanReverse16(Processor p, out ushort index, ushort value)
        {
            for(int i = 15; i >= 0; i--)
            {
                if((value & (1 << i)) != 0)
                {
                    index = (ushort)i;
                    p.Flags &= ~EFlags.Zero;
                    return;
                }
            }

            index = 0;
            p.Flags |= EFlags.Zero;
        }

        [Alternate("BitScanReverse16", OperandSize = 32, AddressSize = 16 | 32)]
        public static void BitScanReverse32(Processor p, out uint index, uint value)
        {
            for(int i = 31; i >= 0; i--)
            {
                if((value & (1 << i)) != 0)
                {
                    index = (uint)i;
                    p.Flags &= ~EFlags.Zero;
                    return;
                }
            }

            index = 0;
            p.Flags |= EFlags.Zero;
        }
    }
}
